package com.test.book.service;

import com.test.book.dao.BookDao;
import com.test.book.mapper.BookMapper;
import com.test.book.model.BookInfo;
import com.test.book.model.PageRequest;
import com.test.book.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class BookService {

    @Autowired
    BookMapper bookMapper;

    public void addBook(BookInfo bookInfo){
        bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest){
        Integer total = bookMapper.selectCount();
        List<BookInfo> books = bookMapper.selectByPage(pageRequest);
        for (BookInfo book:books){
            if (book.getStatus()==0){
                book.setStatusCN("⽆效");
            }else if (book.getStatus()==1){
                book.setStatusCN("可借阅");
            }else {
                book.setStatusCN("不可借阅");
            }
        }
        return new PageResult<>(total,books,pageRequest);
    }

    public BookInfo queryBookById(Integer id){
        return bookMapper.queryBookById(id);
    }

    public String updateBook(BookInfo bookInfo){
        log.info("更新图书，request:{}",bookInfo);
        if (bookInfo.getId() == null || bookInfo.getId()<= 0){
            log.warn("更新图书参数不合法,request:{}",bookInfo);
            return "参数不合法";
        }
        try{
            bookMapper.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("更新图书异常,e:{}",e);
            return "更新图书发生异常";
        }
    }

    public void batchDeleteBook(List<Integer> ids){
        bookMapper.batchDeleteBook(ids);
    }

}
